"""
    Author:
    Date: 2019-04-28

    微信管理后台-AB测试
"""

import datetime

from django.shortcuts import render

from .. import helpers
from .. helpers import gma_login_required
from entities import user_mgr
from entities.consts import *
from utils import util


@gma_login_required
def index(request):
    """ 首页 """
    # context
    context = helpers.build_context(request)
    return render(request, 'gma/ab/ab_home.html', context)


@gma_login_required
def ab_accost(request):
    """ 系统搭讪 """

    # 测试基本数据
    begin_time = '2019-04-22 19:00:00'
    end_time = '2019-04-30 18:00:00'
    test_name = '系统搭讪 v1.0'
    test_title = '随机挑选在线主播，给用户发送私信'
    test_desp = [' - 每一分钟发送一条', ' - 给新用户发送五条信息', ' - 给老用户发送两条']
    team_a_desp = '发送私信'
    team_b_desp = '不发送私信'
    return get_compare_data(request, begin_time, end_time, TEAM_A, TEAM_B, test_name, test_title, test_desp, team_a_desp, team_b_desp)


@gma_login_required
def ab_accost2(request):
    """ 系统搭讪 """

    # 测试基本数据
    begin_time = '2019-04-30 18:00:00'
    end_time = util.datetimetostr(datetime.datetime.now())
    test_name = '系统搭讪 v2.0'
    test_title = '随机挑选在线主播，给用户发送私信'
    test_desp = [' - 每一分钟发送一条', ' - 给新用户发送五条信息', ' - 给老用户发送两条']
    team_a_desp = '发送私信'
    team_b_desp = '不发送私信'
    return get_compare_data(request, begin_time, end_time, TEAM_A, TEAM_B, test_name, test_title, test_desp, team_a_desp, team_b_desp)


@gma_login_required
def ab_auto_follow(request):
    """ 自动关注主播 """

    # 测试基本数据
    begin_time = '2019-04-30 18:50:00'
    end_time = util.datetimetostr(datetime.datetime.now())
    test_name = '自动关注主播'
    test_title = '关注推荐榜单中在线的前5位主播'
    test_desp = []
    team_a_desp = '自动关注'
    team_b_desp = '不自动关注'
    return get_compare_data(request, begin_time, end_time, TEAM_C, TEAM_D, test_name, test_title, test_desp, team_a_desp, team_b_desp)


def get_compare_data(request, begin_time, end_time, team_a, team_b, test_name, test_title, test_desp, team_a_desp, team_b_desp):
    """ 获取搭讪AB测试的数据 """
    data = []

    new_caster_count_a = user_mgr.get_new_caster_count(begin_time, end_time, None, team_a)
    new_user_count_a = user_mgr.get_new_user_count(begin_time, end_time, None, team_a, request=request) - new_caster_count_a
    paid_new_user_count_a = user_mgr.get_paid_new_user_count(begin_time, end_time, None, team_a)
    repaid_new_user_count_a = user_mgr.get_repaid_new_user_count(begin_time, end_time, None, team_a)
    paid_amount_a = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_a)
    paid_amount_30_a = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_a, 30)
    paid_amount_98_a = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_a, 98)
    paid_amount_298_a = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_a, 298)
    paid_amount_588_a = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_a, 588)
    paid_amount_a388_a = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_a, 1388)
    rpur_a = float('%.2f' % ((100*repaid_new_user_count_a/paid_new_user_count_a) if paid_new_user_count_a > 0 else 0))
    pur_a = float('%.2f' % ((100*paid_new_user_count_a/new_user_count_a) if new_user_count_a > 0 else 0))
    arpu_a = float('%.2f' % ((paid_amount_a/new_user_count_a) if new_user_count_a > 0 else 0))
    arppu_a = float('%.2f' % (int(paid_amount_a / paid_new_user_count_a) if paid_new_user_count_a > 0 else 0))

    new_caster_count_b = user_mgr.get_new_caster_count(begin_time, end_time, None, team_b)
    new_user_count_b = user_mgr.get_new_user_count(begin_time, end_time, None, team_b, request=request) - new_caster_count_b
    paid_new_user_count_b = user_mgr.get_paid_new_user_count(begin_time, end_time, None, team_b)
    repaid_new_user_count_b = user_mgr.get_repaid_new_user_count(begin_time, end_time, None, team_b)
    paid_amount_b = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_b)
    paid_amount_30_b = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_b, 30)
    paid_amount_98_b = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_b, 98)
    paid_amount_298_b = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_b, 298)
    paid_amount_588_b = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_b, 588)
    paid_amount_1388_b = user_mgr.get_new_user_pay_amount(begin_time, end_time, None, team_b, 1388)
    rpur_b = float('%.2f' % ((100*repaid_new_user_count_b/paid_new_user_count_b) if paid_new_user_count_b > 0 else 0))
    pur_b = float('%.2f' % ((100*paid_new_user_count_b/new_user_count_b) if new_user_count_b > 0 else 0))
    arpu_b = float('%.2f' % ((paid_amount_b/new_user_count_b) if new_user_count_b > 0 else 0))
    arppu_b = float('%.2f' % (int(paid_amount_b / paid_new_user_count_b) if paid_new_user_count_b > 0 else 0))


    data.append(('新增用户', new_user_count_a, new_user_count_b, '', '', False))
    data.append(('付费用户', paid_new_user_count_a, paid_new_user_count_b, '', '', True))
    data.append(('复充用户', repaid_new_user_count_a, repaid_new_user_count_b, '', '', True))
    data.append(('充值30', paid_amount_30_a, paid_amount_30_b, '￥', '', True))
    data.append(('充值98', paid_amount_98_a, paid_amount_98_b, '￥', '', True))
    data.append(('充值298', paid_amount_298_a, paid_amount_298_b, '￥', '', True))
    data.append(('充值588', paid_amount_588_a, paid_amount_588_b, '￥', '', True))
    data.append(('充值1388', paid_amount_a388_a, paid_amount_1388_b, '￥', '', True))
    data.append(('---', '---', '---', '', '', False))
    data.append(('❗累计️充值', paid_amount_a, paid_amount_b, '￥', '', True))
    data.append(('❗️付费渗透率', pur_a, pur_b, '', '%', True))
    data.append(('❗️复充率', rpur_a, rpur_b, '', '%', True))
    data.append(('❗️ARPU', arpu_a, arpu_b, '￥', '', True))
    data.append(('❗️ARPPU', arppu_a, arppu_b, '￥', '', True))

    # context
    context = helpers.build_context(request)
    context['test_name'] = test_name
    context['test_title'] = test_title
    context['test_desp'] = test_desp
    context['team_a_desp'] = team_a_desp
    context['team_b_desp'] = team_b_desp
    context['begin_time'] = util.strtodatetime(begin_time)
    context['end_time'] = util.strtodatetime(end_time)
    context['data'] = data
    return render(request, 'gma/ab/ab_accost.html', context)


if __name__ == '__main__':
    print('ok')
